package com.tju.resource.server.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tju.resource.mapper.TbShapRevMapper;
import com.tju.resource.model.TbShapRev;
import com.tju.resource.server.TbShapRevService;
import com.tju.resource.tool.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.tju.resource.tool.result.ResultUtil.*;
import static com.tju.resource.tool.result.ResultEnum.*;
import static com.tju.resource.tool.tool.QuickQuery.QueryType.EQUALS;
import static com.tju.resource.tool.tool.QuickQuery.QueryType.LIKE;
import static com.tju.resource.tool.tool.QuickQuery.page;
import static com.tju.resource.tool.tool.QuickQuery.param;

/**
 *
 */
@Service
public class TbShapRevServiceImpl implements TbShapRevService {

    @Autowired
    TbShapRevMapper mapper;

    @Override
    @Transactional
    public Result<List<TbShapRev>> byShap(JSONObject params) {
        Result<List<TbShapRev>> ret = new Result<>();

        try {
            int page = params.getInt("page");
            int limit = params.getInt("limit");
            IPage<TbShapRev> result = page(
                    mapper
                    , page
                    , limit
                    , param(EQUALS, "shap_id", params.get("shapId"))
                    , param(LIKE, "shap_rev", params.get("shapRev")));

            ret.setCode(200);
            ret.setMsg("done.");
            ret.setData(result.getRecords());
            ret.setCount(result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            ret.setData(new ArrayList<>());
            ret.setCount(500);
            ret.setMsg(e.toString());
        }
        return ret;
    }

    @Override
    @Transactional
    public Result<Object> add(TbShapRev tbShapRev) {
        int insert = mapper.insert(tbShapRev);
        return insert > 0 ? success(SUCCESS, insert) : error(UNKONW_ERROR, "insert failed.");
    }

    @Override
    @Transactional
    public Result<Object> upd(TbShapRev tbShapRev) {
        int update = mapper.updateById(tbShapRev);
        return update > 0 ? success(SUCCESS, update) : error(UNKONW_ERROR, "update failed.");
    }

    @Override
    @Transactional
    public Result<Object> del(Long tbShapRev) {
        int delete = mapper.deleteById(tbShapRev);
        return delete > 0 ? success(SUCCESS, delete) : error(UNKONW_ERROR, "delete failed.");
    }

    @Override
    @Transactional
    public Result<Object> handle(JSONObject data) {
        try {
            JSONArray addData = data.getJSONArray("addData");
            JSONArray editData = data.getJSONArray("editData");
            JSONArray delData = data.getJSONArray("delData");

            addData.forEach(d -> {
                TbShapRev rev = JSONUtil.toBean(JSONUtil.parseObj(d), TbShapRev.class);

                rev.setShapId(data.getLong("shapId"));
                add(rev);
            });
            editData.forEach(d -> {
                TbShapRev rev = JSONUtil.toBean(JSONUtil.parseObj(d), TbShapRev.class);

                rev.setShapId(data.getLong("shapId"));
                upd(rev);
            });
            delData.forEach(d -> del(Long.parseLong(d + "")));

            return success(SUCCESS, "操作完成!");
        } catch (Exception e) {
            return error(UNKONW_ERROR, String.format("操作失败(%s)", e));
        }
    }
}
